package breeze.pixel.weather.apps_util.utils.appsettings.settingsview;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.Editable;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.Switch;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.google.android.material.appbar.MaterialToolbar;

import breeze.pixel.weather.App;
import breeze.pixel.weather.R;
import breeze.pixel.weather.apps_util.utils.MToast;
import breeze.pixel.weather.apps_util.utils.TTSUtils;
import breeze.pixel.weather.apps_util.utils.appsettings.AppSettings;
import breeze.pixel.weather.apps_util.views.mtextview.MTextView;

public class TTSControlView extends AppCompatActivity {

    private EditText editText;

    private MTextView adjust_speed, adjust_yudiao;

    private SeekBar seek_speed, seek_yudiao;

    private TTSUtils ttsUtils = TTSUtils.getInstance(this);

    private AppSettings sets = App.getAppSettings();

    @SuppressLint("UseSwitchCompatOrMaterialCode")
    private Switch control;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_t_t_s_control_view);
        init();
        initData();
    }

    public void init() {
        MaterialToolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        editText = findViewById(R.id.tts_control_editext);
        adjust_speed = findViewById(R.id.tts_control_speed_adjust_tv);
        adjust_yudiao = findViewById(R.id.tts_control_yudiao_adjust_tv);
        seek_speed = findViewById(R.id.tts_control_speed_adjust);
        seek_yudiao = findViewById(R.id.tts_control_yudiao_adjust);
        control = findViewById(R.id.tts_control_isUsingTTS);
    }

    public void initData() {
        control.setChecked(sets.IsUsingSpeak());
        editText.setText(sets.getSpeakContent());
        seek_speed.setProgress((int) (sets.getSpeakSpeed() * 10));
        adjust_speed.setText(String.format("语速调整:%s", sets.getSpeakSpeed()));
        seek_yudiao.setProgress((int) (sets.getSpeakPitch() * 10));
        adjust_yudiao.setText(String.format("语调调整:%s", sets.getSpeakPitch()));
        seek_yudiao.setMax(20);
        seek_yudiao.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                float yudiao = (float) progress / 10;
                adjust_yudiao.setText(String.format("语调调整:%s", yudiao));
                sets.setSpeakPitch(yudiao);
                ttsUtils.setPitch(yudiao);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        seek_speed.setMax(20);
        seek_speed.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                float speed = (float) progress / 10;
                adjust_speed.setText(String.format("语速调整:%s", speed));
                sets.setSpeakSpeed(speed);
                ttsUtils.setSpeed(speed);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        control.setOnCheckedChangeListener((buttonView, isChecked) -> sets.setIsUsingSpeak(isChecked));
    }

    public void try_listen(View view) {
        String str = editText.getText().toString();
        ttsUtils.speak(str);
    }

    public void save_data(View view) {
        String str = editText.getText().toString();
        sets.setSpeakContent(str);
        MToast.show(this,"保存成功");
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        int itemId = item.getItemId();
        if (itemId == android.R.id.home){
            finish();
        } else if (itemId == R.id.menu_tts_insert){
            if (!sets.isDonated()){
                MToast.show(this,"仅高级版可用");
            }
        } else if (itemId == R.id.menu_tts_now_weather){
            append("{{weather}}");
        } else if (itemId == R.id.menu_tts_now_temp){
            append("{{temperature}}");
        } else if (itemId == R.id.menu_tts_now_wind){
            append("{{wind}}");
        } else if (itemId == R.id.menu_tts_now_humidity){
            append("{{humidity}}");
        } else if (itemId == R.id.menu_tts_now_windSpeed){
            append("{{windSpeed}}");
        } else if (itemId == R.id.menu_tts_now_air){
            append("{{airCategory}}");
        } else if (itemId == R.id.menu_tts_now_city){
            append("{{city}}");
        }
        return super.onOptionsItemSelected(item);
    }

    private void append(String str){
        if (!sets.isDonated()){
            MToast.show(this,"仅高级版可用");
        }else {
            int selectionStart = editText.getSelectionStart();
            Editable text = editText.getText();
            text.insert(selectionStart,str);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_tts_tool,menu);
        return super.onCreateOptionsMenu(menu);
    }
}